package rcp.iphex2.ui.heatmapexplorer;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Panel;

import javax.swing.JRootPane;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.renderer.PaintScale;
import org.jfree.chart.renderer.xy.XYBlockRenderer;

public class ChartTools {
	
	public ChartTools() {
		// TODO Auto-generated constructor stub
	}
	
	
	// A XYBlockRenderer with an included HeatMap PaintScale
	public static XYBlockRenderer createHeatmapRenderer(int inititalAmplifier,
			double avarageK0Distance, double avarageTimeDistance) {
		PaintScale ps = new WBRG_PaintScale(0, inititalAmplifier);
		XYBlockRenderer xybr = new XYBlockRenderer();
		xybr.setPaintScale(ps);
		xybr.setDataBoundsIncludesVisibleSeriesOnly(true);
		xybr.setBlockWidth(avarageK0Distance);
		xybr.setBlockHeight(avarageTimeDistance);
		return xybr;
	}

	// Enables the adding of a SwingJFreechart component on a SWT composite
	public static ChartPanel addChartToCompoiste(JFreeChart chart,Composite parent) {
		ChartPanel cp = new ChartPanel(chart);
		Composite adapterComposite = new Composite(parent, SWT.NO_BACKGROUND | SWT.EMBEDDED);
		
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.verticalAlignment = GridData.FILL;
		gridData.grabExcessVerticalSpace = true;
		adapterComposite.setLayoutData(gridData);
		
		
		
		try {
			System.setProperty("sun.awt.noerasebackground", "true");
		} catch (NoSuchMethodError error) {
		}
		/* Create and setting up frame */
		Frame frame = SWT_AWT.new_Frame(adapterComposite);
		@SuppressWarnings("serial")
		Panel panel = new Panel(new BorderLayout()) {
			@Override
			public void update(java.awt.Graphics g) {
				/* Do not erase the background */
				paint(g);
			}
		};

		frame.add(panel);
		JRootPane root = new JRootPane();
		panel.add(root);
		java.awt.Container contentPane = root.getContentPane();
		contentPane.add(cp);

		

		
		
		
		return cp;
	}
}
